package com.easyctrl.adapter;

import java.util.ArrayList;

import android.content.Context;
import android.view.View;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;

import com.easyctrl.iface.FRMAdapterListener;
import com.easyctrl.ldy.domain.DeviceBean; 

public class DeviceAdapter extends FRMAdapter<DeviceBean> implements FRMAdapterListener { 
	
	private static DeviceAdapter instance; 
	private ArrayList<DeviceBean> deletes;  
	
	private DeviceAdapter(Context context){ 
		super(context); 
		deletes = new ArrayList<DeviceBean>();
		setAdapterListener(this);
	}
	public static synchronized DeviceAdapter getInstance(Context context){
		if(instance == null){
			instance = new DeviceAdapter(context);
		}
		return instance;
	}
	public ArrayList<DeviceBean> getDeletes() {
		return deletes;
	}
	@Override
	public void onGetView(com.easyctrl.adapter.FRMAdapter.ViewHolder holder,int position, ArrayList beans) { 
		DeviceBean bean = (DeviceBean) beans.get(position); 
		
		holder.name.setTag(bean);
		holder.delete.setTag(bean);
		
		holder.delete.setChecked(deletes.contains(bean));
		
		holder.name.setText(bean.device_name);  
		holder.num.setText(""+(position+1)); 
		
		if(bean.device_isSystem == 1){ 
			holder.delete.setVisibility(View.GONE);
			holder.use.setVisibility(View.GONE);
		}else{ 
			holder.delete.setVisibility(View.VISIBLE);
			holder.use.setVisibility(View.GONE);
		}
		
		
		holder.delete.setOnCheckedChangeListener(new OnCheckedChangeListener() {
			
			@Override
			public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { 
				DeviceBean bean = (DeviceBean) buttonView.getTag();
				if(isChecked){ 
					deletes.add(bean);
				}else{
					deletes.remove(bean);
				}
			}
		});
	}  
}
